package com.example.myapplication5;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int DURATION = 70;
    private ImageView mImage;
    private Button mBtnStart;
    private Button mBtnStop;
    private Button mBtnNext;
    private AnimationDrawable mAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mImage = findViewById(R.id.image);
        mAnimation = (AnimationDrawable) mImage.getDrawable();
        //initAnimation();
        //mImage.setImageDrawable(mAnimation);
        mBtnStart = findViewById(R.id.btn_start);
        mBtnStop = findViewById(R.id.btn_stop);
        mBtnNext = findViewById(R.id.btn_next);
        mBtnStart.setOnClickListener(this);
        mBtnStop.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.e("test123", "==========onPause==========");
        stopAnimation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("test123", "==========onResume==========");
        startAnimation();
    }

    private void startAnimation() {
        if (mAnimation != null) {
            mAnimation.start();
        }
    }

    private void stopAnimation() {
        if (mAnimation != null) {
            mAnimation.stop();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_start) {
            startAnimation();
        } else if (id == R.id.btn_stop) {
            stopAnimation();
        } else if (id == R.id.btn_next) {
            Intent intent = new Intent(MainActivity.this, AnimationActivity.class);
            startActivity(intent);
            //overridePendingTransition(R.anim.scale_in, R.anim.no_anim);
        }
    }
    private void initAnimation() {
        mAnimation = new AnimationDrawable();
        mAnimation.addFrame(getDrawable(R.drawable.f1), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f2), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f3), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f4), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f5), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f6), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f7), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f8), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f9), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f10), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f11), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f12), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f13), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f14), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f15), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f16), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f17), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f18), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f19), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f20), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f21), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f22), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f23), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f24), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f25), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f26), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f27), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f28), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f29), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f30), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f31), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f32), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f33), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f34), DURATION);
        mAnimation.addFrame(getDrawable(R.drawable.f35), DURATION);
    }
}